package com.dati.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.TotalClass;
import util.UtilDAO;

import com.dati.bean.Answer;
import com.dati.bean.Daan;
import com.dati.bean.Dati;
import com.dati.bean.DatiMiddle;
import com.dati.bean.SendDati;
import com.dati.bean.setTime;

public class DatiDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-4
	 *下午02:42:01
	 *zzClub
	 *com.dati.dao
	 */
	private ResultSet rs =null;
	private Statement st = null;
	private PreparedStatement pt = null;
	private String sql = "";
	private List<Dati> datiList = null;
	private List<DatiMiddle> datiMiddle = null;
	private List<SendDati> sendDatiList = null;
	private List<Daan> daanList = null;
	public DatiDao(){
		
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-13
	 *上午10:51:23
	 *zzClub
	 *com.dati.dao
	 *List<Dati>
	 */
	public List<Dati> getDatiList(String table,String order,String condition){
		sql = UtilDAO.getList(table, order, condition);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(datiList==null){
					datiList = new ArrayList<Dati>();
				}
				datiList.add(this.getDati(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datiList;
	}
	public Dati getDati(ResultSet rs){
		Dati dati = new Dati();
		try {
			dati.setDatiId(rs.getInt("datiId"));
			dati.setSendPhone(rs.getString("sendPhone"));
			dati.setSmscontent(rs.getString("smscontent"));
			dati.setState(rs.getInt("state"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dati;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-13
	 *上午11:28:36
	 *zzClub
	 *com.dati.dao
	 *List<DatiMiddle>
	 */
	public List<DatiMiddle> getDatiMiddle(String table,String order,String condition){
		sql = UtilDAO.getList(table, order, condition);
		System.out.println("sql$$$$$$$$$$$"+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(datiMiddle==null){
					datiMiddle = new ArrayList<DatiMiddle>();
				}
				datiMiddle.add(this.getDatiMiddle(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datiMiddle;
	}
	public DatiMiddle getDatiMiddle(ResultSet rs){
		DatiMiddle datiMiddle = new DatiMiddle();
		try {
			datiMiddle.setAnswerContent(rs.getString("answerContent"));
			datiMiddle.setAnswerMiddleId(rs.getString("answerMiddleId"));
			datiMiddle.setCreateTime(rs.getString("createTime"));
			datiMiddle.setRightAnswer(rs.getString("rightAnswer"));
			datiMiddle.setSeriseId(rs.getString("seriseId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datiMiddle;
	}
	public String getSetTime(){
		sql="select * from setTime";
		rs = new TotalClass().getResult(sql);
		String time ="";
		try {
			if(rs.next()){
				time = rs.getString("setTime");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return time;
	}
	/**
	 * 把答题 的类型状态修改成1
	 *@author qingyu zhang
	 *@function:
	 * @param datiId
	 * @return
	 *2011-4-13
	 *下午01:50:59
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean modifyDati(String datiId){
		boolean bo= false;
		sql = "update tb_send_dati set state =1 where datiId in("+datiId+")";
		st= new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param id
	 * @return
	 *2011-4-13
	 *下午02:28:12
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean modifySendDati(String middleId,String phone){
		boolean bo = false;
		sql="update sendDati set state=1 where answerMiddleId='"+middleId+"' and state=0 and phone='"+phone+"'";
		System.out.println("修改状态"+sql);
		st = new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo ;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据条件得到sendDati中状态为1 的记录
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-13
	 *下午02:52:42
	 *zzClub
	 *com.dati.dao
	 *List<SendDati>
	 */
	public List<SendDati> getSendDati(String table,String order,String condition){
		sql = UtilDAO.getList(table, order, condition);
		System.out.println("sql##########"+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(sendDatiList==null){
					sendDatiList = new ArrayList<SendDati>();
				}
				sendDatiList.add(this.getSendDati(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendDatiList;
	}
	public SendDati getSendDati(ResultSet rs){
		SendDati sendDati = new SendDati();
		try {
			sendDati.setAnswerMiddleId(rs.getString("answerMiddleId"));
			sendDati.setCreateTime(rs.getString("createTime"));
			sendDati.setEndTime(rs.getString("endTime"));
			sendDati.setId(rs.getInt("id"));
			sendDati.setPhone(rs.getString("phone"));
			sendDati.setRightOrno(rs.getInt("rightOrno"));
			sendDati.setSeriseId(rs.getString("seriseId"));
			sendDati.setState(rs.getInt("state"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendDati;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:得到上传上来的题目答案
	 * @param table
	 * @param condition
	 * @param order
	 * @return
	 *2011-4-13
	 *下午04:45:42
	 *zzClub
	 *com.dati.dao
	 *List<Daan>
	 */
	public List<Daan> getDaanList(String table,String condition,String order){
		sql = UtilDAO.getList(table, order, condition);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(daanList==null){
					daanList = new ArrayList<Daan>();
				}
				daanList.add(this.getDaan(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return daanList;
	}
	public Daan getDaan(ResultSet rs){
		Daan daan = new Daan();
		try {
			daan.setDaAn(rs.getString("daAn"));
			daan.setId(rs.getInt("id"));
			daan.setPhone(rs.getString("phone"));
			daan.setState(rs.getInt("state"));
			daan.setTimuId(rs.getString("timuId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return daan;
	}
	/**
	 *  在分拣时根据手机号码与上传的信息ID得到此记录的中间表ID
	 *@author qingyu zhang
	 *@function:
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-14
	 *上午11:46:36
	 *zzClub
	 *com.dati.dao
	 *List<Answer>
	 */
	public List<Answer> getAnswerList(String table,String order,String condition){
		List<Answer> answerList = null;
		sql = UtilDAO.getList(table, order, condition);
		System.out.println("搜索"+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(answerList==null){
					answerList =new ArrayList<Answer>();
				}
				answerList.add(this.getAnswer(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return answerList;
	}
	public Answer getAnswer(ResultSet rs){
		Answer answer = new Answer();
		try {
			answer.setAnswerId(rs.getInt("answerId"));
			answer.setAnswerIntegral(rs.getString("answerIntegral"));
			answer.setAnswerMiddleId(rs.getString("answerMiddleId"));
			answer.setAnswerUpPhone(rs.getString("answerUpPhone"));
			answer.setAnswerUpTime(rs.getString("answerUpTime"));
			answer.setReceiveId(rs.getInt("receiveId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return answer;
	}
	public boolean saveDatiMiddle(DatiMiddle middle){
	
		sql="insert into tb_answer_middle (answerMiddleId,seriseId,answerContent,createTime,rightAnswer) " +
				"values('"+middle.getAnswerMiddleId()+"','"+middle.getSeriseId()+"','"+middle.getAnswerContent()+"','"+middle.getCreateTime()
				+"','"+middle.getRightAnswer()+"')";
		System.out.println("插入语句"+sql);
		boolean bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	public boolean saveAnswer(Answer answer){
		sql="insert into tb_answer (answerMiddleId,receiveId,answerUpPhone,answerUpTime,answerIntegral) " +
				"values('"+answer.getAnswerMiddleId()+"',"+answer.getReceiveId()+",'"+answer.getAnswerUpPhone()+"','"+answer.getAnswerUpTime()+"','"+answer.getAnswerIntegral()+"')";
		boolean bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	/**
	 * 批量导入中间表数据
	 *@author qingyu zhang
	 *@function:
	 * @param middleList
	 * @return
	 *2011-4-18
	 *下午10:01:06
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean saveMiddle(List<DatiMiddle> middleList){
		boolean bo = false;
		st = new TotalClass().getStatement();
		try{
			for(DatiMiddle middle:middleList){
				String hql = "insert into tb_answer_middle (answerMiddleId,seriseId,answerContent,createTime,rightAnswer) " +
						"values('"+middle.getAnswerMiddleId()+"','"+middle.getSeriseId()+"','"+middle.getAnswerContent()+"','"+middle.getCreateTime()+"','"+middle.getRightAnswer()+"')";
				st.addBatch(hql);
			}
			st.executeBatch();
			bo = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return bo;
	}
	/**
	 * 批量导入答题
	 *@author qingyu zhang
	 *@function:
	 * @param answerList
	 * @return
	 *2011-4-18
	 *下午10:06:05
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean saveAnswer(List<Answer> answerList){
		boolean bo = false;
		st = new TotalClass().getStatement();
		try{
			for(Answer answer:answerList){
				String hql = "insert into tb_answer (answerMiddleId,receiveId,answerUpPhone,answerUpTime,answerIntegral) " +
				"values('"+answer.getAnswerMiddleId()+"',"+answer.getReceiveId()+",'"+answer.getAnswerUpPhone()+"','"+answer.getAnswerUpTime()+"','"+answer.getAnswerIntegral()+"')";
				st.addBatch(hql);
			}
			st.executeBatch();
			bo = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return bo;
	}
	/**
	 * 得到设置答题 的时间
	 *@author qingyu zhang
	 *@function:
	 * @return
	 *2011-4-19
	 *上午10:38:34
	 *zzClub
	 *com.dati.dao
	 *setTime
	 */
	public setTime getSetTime1(){
		setTime setT = new setTime();
		sql="select * from setTime where id=1";
		rs = new TotalClass().getResult(sql);
		try {
			if(rs.next()){
				setT = this.getSetTime(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return setT;
	}
	
	public setTime getSetTime(ResultSet rs){
		setTime setT = new setTime();
		try {
			setT.setId(rs.getInt("id"));
			setT.setSetTime(rs.getString("setTime"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return setT;
	}
	/**
	 * 设置答题时间
	 *@author qingyu zhang
	 *@function:
	 * @param setT
	 * @return
	 *2011-4-19
	 *上午10:55:39
	 *zzClub
	 *com.dati.dao
	 *boolean
	 */
	public boolean setTim(setTime setT){
		boolean bo = false;
		sql = "update setTime set setTime='"+setT.getSetTime()+"' where id="+setT.getId();
		st = new TotalClass().getStatement();
		try {
			bo = st.execute(sql);
			bo = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   return bo;
	}
}
